package Redis缓存数据.GEO;


//把数据库中的数据写入到redis
//tomcat启动时执行

import com.ruoyi.charge.common.GlobalConstant;
import com.ruoyi.charge.domain.ChargingStation;
import com.ruoyi.charge.service.IChargingStationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.BoundGeoOperations;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class InitData {

    @Autowired
    IChargingStationService chargingStationService;

    @Autowired
    RedisTemplate redisTemplate;

    //tomcat启动时执行
    @PostConstruct
    public void readDBStationDataWriteToRedis() {
        System.out.println("tomcat启动完后自己执行");
        BoundGeoOperations boundGeoOperations = redisTemplate.boundGeoOps(GlobalConstant.STATION_GEO_ZSET);
        BoundHashOperations boundHashOperations = redisTemplate.boundHashOps(GlobalConstant.STATION_DETAIL_HASH);

        Map<String, Point> hashMap = new HashMap<>();
        //null表示查询条件，没有查询条件，就是查询所有
        List<ChargingStation> stationList = chargingStationService.selectChargingStationList(null);
        for (ChargingStation chargingStation : stationList) {
            double longitude = Double.parseDouble(chargingStation.getStationLng().toString());
            double latitude = Double.parseDouble(chargingStation.getStationLat().toString());
            String id = chargingStation.getId().toString();
            Point point = new Point(longitude, latitude);
            hashMap.put(id, point);

            //一次写一个数据
            boundHashOperations.put(id, chargingStation);
        }

        //批处理，一次写入6个数据
        boundGeoOperations.add(hashMap);

    }
}
